﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.IP地址和真实ip
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = GetUserIP();
            Label2.Text = "lol";

            var requestIP = HttpContext.Current.Request.UserHostAddress;
            var serverIP = HttpContext.Current.Request.ServerVariables.Get("Local_Addr").ToString();
            Label3.Text = requestIP;
            Label4.Text = serverIP;
        }

        private string GetUserIP()
        {
            string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (!string.IsNullOrEmpty(ipList))
            {
                return ipList.Split(',')[0];
            }

            return Request.ServerVariables["REMOTE_ADDR"];
        }
    }
}